package com.avic.cplustest;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    static {
        System.loadLibrary("native-lib");
    }

    private Button button;
    private Button button2;
    private Button button3;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // Example of a call to a native method
        TextView tv = (TextView) findViewById(R.id.sample_text);
        tv.setText(stringFromJNI() + helloWang());
        button = (Button) findViewById(R.id.button);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                testCall();
            }
        });
        button2 = (Button) findViewById(R.id.button2);
        button2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                nativeMehtodCallJavaProperty();
            }
        });
        button3 = (Button) findViewById(R.id.button3);
        button3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                callSuperInstanceMethod();
            }
        });
    }


    /**
     * native call java constructor
     */
    public native static void callSuperInstanceMethod();


    /**
     * Native 方法改变Java类属性或者成员变量
     */
    private void nativeMehtodCallJavaProperty() {
        ClassField obj = new ClassField();
        obj.setNum(10);
        obj.setStr("hello");

        //本地代码访问和修改ClassField中的静态属性num
        accessStaticField();
        accessInstanceField(obj);

        //输出本地代码修改过后的值
        System.out.println("In Java------>ClassField.num = " + obj.getNum());
        System.out.println("In Java------>ClassField.str = " + obj.getStr());

    }

    private native static void accessInstanceField(ClassField obj);

    private native static void accessStaticField();


    /**
     * Native方法使用JNI的反射函数调用Java中的实例方法和静态方法
     */
    private void testCall() {
        callJavaInstanceMethod();
        callJavaStaticMethod();
    }


    public static native void callJavaStaticMethod();

    public static native void callJavaInstanceMethod();

    /**
     * A native method that is implemented by the 'native-lib' native library,
     * which is packaged with this application.
     */
    public native String stringFromJNI();

    public native String helloWang();


}


































